package com.ay.weixin.fdc.front.share.event;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;

import org.lizar.controller.Event;
import org.lizar.controller.loader.EventLoader;
import org.lizar.ioc.Container;
import org.lizar.util.StringHelper;

import com.ay.weixin.config.ProjectConfig;
import com.ay.weixin.fdc.boss.share.db.ShareActivityContentDao;
import com.ay.weixin.fdc.boss.share.db.ShareActivityDao;
import com.ay.weixin.fdc.boss.share.db.ShareBackHitsDao;
import com.ay.weixin.fdc.boss.share.db.ShareRecordDao;
import com.ay.weixin.fdc.boss.share.domain.ShareActivity;
import com.ay.weixin.fdc.boss.share.domain.ShareActivityContent;
import com.ay.weixin.fdc.boss.share.domain.ShareBackHits;
import com.ay.weixin.fdc.boss.share.domain.ShareBackHitsDetail;
import com.ay.weixin.fdc.boss.share.domain.ShareRecord;
import com.ay.weixin.fdc.boss.user.db.UserDao;
import com.ay.weixin.fdc.boss.user.domain.UserBasic;
import com.ay.weixin.fdc.boss.user.domain.UserWeiXinInfo;
import com.ay.weixin.fdc.front.usercenter.event.UserInfoAccess;

public class ShareEvt extends Event {

	private ShareActivityDao share_dao;
	private ShareActivityContentDao content_dao;
	private UserDao user_dao;
	private ShareBackHitsDao backhits_dao;
	private UserInfoAccess user_access;
	private ShareRecordDao share_record_dao;
	
	@Override
	public void init_property() throws Exception {
		share_dao=Container.get(ShareActivityDao.class);
		user_dao=Container.get(UserDao.class);
		backhits_dao=Container.get(ShareBackHitsDao.class);
		content_dao=Container.get(ShareActivityContentDao.class);
		user_access=Container.get(UserInfoAccess.class);
		share_record_dao=Container.get(ShareRecordDao.class);
		
		
		
	}

	@Override
	public String default_handling_path() {
		return "/share/*";
	}

	@Override
	public void handle(EventLoader el) throws ServletException, IOException {
		long wxproject_id=el.queryPathInt(1);
		
		if(!el.getBool("is_weixin")&&ProjectConfig._bool(wxproject_id,"sys.weixin_only")){
			el.text("invalid visit");
			return;
		}
		UserBasic user=el.getSessionAttr("user",UserBasic.class);
		if(el.queryPathCheck(2, "list")){
			list(el,wxproject_id);
		}else if(el.queryPathCheck(2, "blog")){
			try {
				blog(el,wxproject_id);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return;
		}else if(el.queryPathCheck(2, "myinfo")){
			if(user==null){
				el.response_to_root("/share/register/"+wxproject_id);
				return;
			}
			myinfo(el,wxproject_id);
			return;
		}else if(el.queryPathCheck(2, "myshare")){
			if(user==null){
				el.response_to_root("/share/register/"+wxproject_id);
				return;
			}
			try {
				myshare(el,wxproject_id);
			} catch (SQLException e) {
				e.printStackTrace();
				list(el,wxproject_id);
				return;
			}
			return;
		}else{
			if(el.getSessionAttr("user")==null){
				el.response_to_root("/share/register/"+wxproject_id);
				return;
			}
		}
		list(el,wxproject_id);
	}
	
	private void myshare(EventLoader el,long project_id) throws IOException, ServletException, SQLException{
		
		UserBasic user=el.getSessionAttr("user",UserBasic.class);
		
		List<ShareBackHitsDetail> hits_list=backhits_dao.getMyShareHitsDetail(user.getId());
		
		el.setAttr("hits_list", hits_list);
		el.setAttr("status_list", ShareActivity.STATUS.values());
		el.template("/WEB-INF/template/app/"+project_id+"/share/myshare.vm");
	}
	
	private void myinfo(EventLoader el,long project_id) throws IOException, ServletException{
		el.template("/WEB-INF/template/app/"+project_id+"/share/myinfo.vm");
	}
	
	private void blog(EventLoader el,long wxproject_id) throws SQLException, IOException, ServletException{
		long uid=el.getLong("uid");
		long sa_id=el.getLong("sa_id");
		String view_id=null;
		if(StringHelper.equals(ProjectConfig.getStr(wxproject_id,"share.backhits_by_ip_or_openid"),"openid")){
			//微信授权
			UserWeiXinInfo weixin_info=el.getSessionAttr("weixin_info",UserWeiXinInfo.class);
			if(weixin_info==null){
				user_access.open_id_check(el,"/share/blog/",wxproject_id,"?uid="+uid+"&sa_id="+sa_id);
				return;
			}else{
				view_id=weixin_info.getId();
			}
		}else{
			view_id=el.getClientIP();
		}
		
		ShareActivity sa=share_dao.getById(sa_id);
		
		if(sa==null){
			System.out.println("sa is null");
			list(el,wxproject_id);
			return;
		}
		List<ShareActivityContent> content_list=null;
		content_list=content_dao.getBySAId(sa_id);
		el.setAttr("content_list", content_list);
		el.setAttr("sa", sa);
		
		if(sa.getStatus()!=ShareActivity.STATUS.ON_GOING.getValue()&&sa.getStatus()!=ShareActivity.STATUS.ABOUT_START.getValue()){
			el.template("/WEB-INF/template/app/"+wxproject_id+"/share/blog/"+sa.getTemplate()+"/index.vm");
			return;
		}
		
		boolean exists=share_record_dao.exists(sa_id, view_id);
		System.out.println("sa_id:"+sa_id+" id:"+view_id+" uid:"+uid);
		if(!exists&&sa.getStatus()==ShareActivity.STATUS.ON_GOING.getValue()){
			ShareRecord share_record=new ShareRecord();
			share_record.setOpenid(view_id);
			share_record.setSa_id(sa_id);
			share_record_dao.insert(share_record);
		}
		
		UserBasic from_user=null;
		try {
			from_user=user_dao.getById(uid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		ShareBackHits backhits=null;
		if(from_user!=null){
			backhits=backhits_dao.getByIds(uid,sa_id);
			if(backhits==null){
				backhits=new ShareBackHits();
				backhits.setBack_hits(0);
				backhits.setReward(null);
				backhits.setSa_id(sa_id);
				backhits.setUser_id(uid);
				backhits_dao.insert(backhits);
			}else{
				if(!exists&&sa.getStatus()==ShareActivity.STATUS.ON_GOING.getValue()){
					backhits_dao.inc_backhits(uid,sa_id,1);
				}
			}
		}
		
		el.template("/WEB-INF/template/app/"+wxproject_id+"/share/blog/"+sa.getTemplate()+"/index.vm");
	}
	
	private void list(EventLoader el,long wxproject_id) throws IOException, ServletException{
		List<ShareActivity> salist=null;
		try {
			salist=share_dao.getAllTop50(wxproject_id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		el.setAttr("status_list",ShareActivity.STATUS.values());
		el.setAttr("salist",salist);
		el.template("/WEB-INF/template/app/"+wxproject_id+"/share/index.vm");
	}

}
